﻿(function () {
  'use strict';
  var controllerId = 'project';
  angular.module('ng4model').controller(controllerId, ['common', '$scope', 'currents', '$location', '$routeParams', 'datacontext', projects]);

  function projects(common, $scope, currents, $location, $routeParams, datacontext) {

    var getLogFn = common.logger.getLogFn;
    var log = getLogFn(controllerId);

    // forward parameters
    var projectId = $routeParams.id;

    var vm = this;
    vm.project = {};
    vm.title = 'Texts for Project'; // View Model

    activate();

    function activate() {
      var promises = [getProject()];
      common.activateController(promises, controllerId)
          .then(function () { log('Activated Projects View'); });
    }

    function getProject() {
      return datacontext.getProject(projectId).then(function (data) {
        currents.project = data;
        return vm.project = data;
      });
    }

    $scope.editProject = function () {
      $location.url("/editproject/" + projectId);
    };

    $scope.addOpus = function () {
      return datacontext.addOpus(projectId).then(function (data) {
        $location.url("/opus/" + data.id);
      });
    };

  }
})();